package com.stormrage.edu.serv.Index.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.index.IdxTrialDao;
import com.stormrage.edu.serv.Index.IdxTrialServ;
import com.stormrage.entity.index.IdxTrial;
import com.stormrage.entity.index.rep.IdxTrialRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: IdxTrialServImpl.java
 * @Package com.stormrage.edu.serv.Index.impl
 * @Description: 试看课件 Service
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Service
public class IdxTrialServImpl implements IdxTrialServ {

	private static final Logger logger = LoggerFactory.getLogger(IdxTrialServImpl.class);

	@Autowired
	private IdxTrialDao idxTrialDao;

	@Override
	public IdxTrial queryById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		IdxTrial entity = idxTrialDao.queryById(map);

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<IdxTrial> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 开始运行 ");

		List<IdxTrial> array = idxTrialDao.queryList();

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 完成运行 ");
		return array;

	}

	@Override
	public List<IdxTrial> queryListForIndex() throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 queryListForIndex 】- 开始运行 ");

		List<IdxTrial> array = idxTrialDao.queryListForIndex();

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 queryListForIndex 】- 完成运行 ");
		return array;

	}

	@Override
	public List<IdxTrialRep> queryRepList() throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 queryRepList 】- 开始运行 ");

		List<IdxTrial> array = idxTrialDao.queryList();

		List<IdxTrialRep> list = new ArrayList<IdxTrialRep>();
		int seq = 1;

		Iterator<IdxTrial> it = array.iterator();
		while (it.hasNext()) {
			IdxTrial trial = it.next();
			Integer right = seq % 3 == 0 ? 0 : 1;
			IdxTrialRep rep = IdxTrialRep.instance(trial, seq, right);
			list.add(rep);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 queryRepList 】- 完成运行 ");
		return list;

	}

}
